package repository

import (
	"context"
	"gitee.com/DonHz/basic-go/webook/internal/domain"
	"gitee.com/DonHz/basic-go/webook/internal/repository/dao"
)

var ErrWaitingSMSNotFound = dao.ErrWaitingSMSNotFound

type AsyncSmsRepository interface{
	Add(ctx context.Context, s domain.AsyncSms) error
	PreemptWaitingSMS(ctx context.Context) (domain.AsyncSms, error)
	ReportScheduleResult(ctx context.Context, id int64, success bool) error
}

type asyncSmsRepository struct {
	dao dao.AsyncSmsDAO
}


func NewAsyncSmsRepository(dao dao.AsyncSmsDAO) AsyncSmsRepository {
	return &asyncSmsRepository{
		dao: dao,
	}
}

func (a asyncSmsRepository) Add(ctx context.Context, s domain.AsyncSms) error {
	return nil
}

func (a asyncSmsRepository) PreemptWaitingSMS(ctx context.Context) (domain.AsyncSms, error) {
	as, err:=a.dao.GetWaitngSMS(ctx)
	if err!=nil{
		return domain.AsyncSms{}, err
	}
	return domain.AsyncSms{
		Id: as.Id,
		//TplId: as.Config.Val.TplId,
		//Numbers:as.Config.Val.Numbers,
		//Args: as.Config.Val.Args,
		RetryMax: as.RetryMax,
	}, nil
}

func (a asyncSmsRepository) ReportScheduleResult(ctx context.Context, id int64, success bool) error {
	if success {
		return a.dao.MarkSuccess(ctx, id)
	}
	return a.dao.MarkFailed(ctx, id)
}
